package com.qf.fmall2203.shopcart.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.fmall2203.shopcart.entity.ShoppingCart;
import com.qf.fmall2203.shopcart.service.IShoppingCartService;
import com.qf.fmall2203.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车  前端控制器
 * </p>
 *
 * @author CM
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/shopcart")
@CrossOrigin
public class ShoppingCartController {

    @Autowired
    IShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public ResultVo addShopCart(@RequestBody ShoppingCart shoppingCart){

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String cartTime = dateFormat.format(new Date());
        shoppingCart.setCartTime(cartTime);
        shoppingCartService.save(shoppingCart);
        return ResultVo.ok();

    }

    @GetMapping("/list")
    public ResultVo shopCartList(Integer userId){

        List<ShoppingCart> list = shoppingCartService.shopCartList(userId);

        return ResultVo.ok(list);

    }

    @PutMapping("/update/{cartId}/{num}")
    public ResultVo updateShopCart(@PathVariable("cartId") Integer cartId,@PathVariable("num") Integer num){

        shoppingCartService.update(new UpdateWrapper<ShoppingCart>().set("cart_num", num).eq("cart_id", cartId));

        return ResultVo.ok();

    }

    @GetMapping("/listbycids")
    public ResultVo listbycids(Integer[] cids){

        List<ShoppingCart> list = shoppingCartService.account(cids);

        return ResultVo.ok(list);

    }

}
